package drongo.model.core
{
	import drongo.model.events.ModelEvent;
	
	import flash.events.EventDispatcher;
	
	public class BaseValue extends EventDispatcher implements IValue
	{
		protected var value:*;
		public function BaseValue()
		{
			super(this);
		}
		
		public function GetValue():*
		{
			return value;
		}
		
		public function SetValue(value:*):void
		{
			this.ChackValue(value);
			var oldValue:*=this.value;
			this.value=value;
			SendEvent(value,oldValue);
		}
		
		protected function SendEvent(newValue:*,oldValue:*):void{
			if(this.hasEventListener(ModelEvent.VALUE_CHANGED)){
				this.dispatchEvent(ModelEvent.Create(ModelEvent.VALUE_CHANGED,oldValue,value));
			}
		}
		
		/**
		 * 检测value是否合法，如果不合法，则在内部抛错！
		 */
		protected function ChackValue(value:*):void{
			
		}
		
		public function Decode(type:uint, data:*):void
		{
			this.SetValue(data);
		}
		
		public function Encode(type:uint, data:*=null):*
		{
			return this.value;
		}
		
		public function Equality(value:IValue):Boolean
		{
			if(this.value==value.GetValue()){
				return true;
			}
			return false;
		}
	}
}